package com.yayohos.locationmanager.base.slice;

import com.yayohos.locationmanager.ResourceTable;
import com.yayohos.locationmanager.SamplePresenter;
import com.yayohos.locationmanager.SampleSlicePage;
import com.yayohos.locationmanager.configuration.Configurations;
import com.yayohos.locationmanager.configuration.LocationConfiguration;
import com.yayohos.locationmanager.helper.LogUtils;

import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Text;
import ohos.location.Location;

/**
 * SampleSlicePageSlice用于演示在AbilitySlice中定位.
 * @date 2021/5/26
 */
public class SampleSlicePageSlice extends LocationBaseAbilitySlice implements SamplePresenter.SampleView {
    private SamplePresenter samplePresenter;
    private Text text;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_sample_slice_page);
        SampleSlicePage sampleSlicePage = (SampleSlicePage) getAbility();
        text = (Text) findComponentById(ResourceTable.Id_text_helloworld);
        Button doLocation = (Button) findComponentById(ResourceTable.Id_do_location);
        doLocation.setClickedListener(component -> getLocation());
        AbilitySliceCallBack abilitySliceCallBack = (requestCode, permissions, grantResults) -> {
            getLocationManager().onRequestPermissionsResult(requestCode, permissions, grantResults);
        };
        samplePresenter = new SamplePresenter(this);
        sampleSlicePage.setAbilitySliceCallBack(abilitySliceCallBack);
    }

    @Override
    public LocationConfiguration getLocationConfiguration() {
        return Configurations.defaultConfiguration("Give me the permission!", "Would you mind to turn GPS on?");
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (samplePresenter != null) {
            samplePresenter.destroy();
        }
    }

    @Override
    public void onLocationChanged(Location location) {
        super.onLocationChanged(location);
        samplePresenter.onLocationChanged(location);
    }

    @Override
    public void onLocationFailed(int type) {
        super.onLocationFailed(type);
        samplePresenter.onLocationFailed(type);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public String getText() {
        return text.getText();
    }

    @Override
    public void setText(String text1) {
        text.setText(text1);
    }

    @Override
    public void updateProgress(String text) {
    }

    @Override
    public void dismissProgress() {
    }

    @Override
    public void onPermissionGranted(boolean alreadyHadPermission) {
        super.onPermissionGranted(alreadyHadPermission);
        LogUtils.logI("onPermissionGranted" + alreadyHadPermission);
    }
}
